package com.jspgou.cms.action.front;

import static com.jspgou.common.page.SimplePage.cpn;
import static com.jspgou.cms.Constants.SHOP_SYS;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.front.FrontHelper;
import com.jspgou.cms.entity.ShopSupplier;
import com.jspgou.cms.manager.ShopSupplierMng;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;

@Controller
public class ShopSupplierAct {

	private static final String SUPPLIERLIST = "tpl.supplierlist";
	private static final String SUPPLIERADD = "tpl.supplieradd";
	private static final String SUPPLIEREDIT = "tpl.supplieredit";

	/**
	 * 循环供应商列表
	 * 
	 * @param pageNo
	 * @param request
	 * @param model
	 * @param merchantid
	 * @return
	 */
	@RequestMapping(value = "/supplierlist.jspx")
	public String list(Integer pageNo, HttpServletRequest request,
			ModelMap model, int merchantid) {

		Pagination pagination = manager.getShopSupplierPage(merchantid + "",
				cpn(pageNo), 5);
		model.addAttribute("pagination", pagination);
		model.addAttribute("merchantid", merchantid);
		Website web = SiteUtils.getWeb(request);
		return web.getTplSys(SHOP_SYS, MessageResolver.getMessage(request,
				SUPPLIERLIST), request);
	}

	/**
	 * 供应商添加页面
	 * 
	 * @param model
	 * @param request
	 * @param merchantid
	 * @return
	 */
	@RequestMapping(value = "/supplieradd.jspx")
	public String add(ModelMap model, HttpServletRequest request, int merchantid) {

		model.addAttribute("merchantid", merchantid);
		Website web = SiteUtils.getWeb(request);
		return web.getTplSys(SHOP_SYS, MessageResolver.getMessage(request,
				SUPPLIERADD), request);
	}

	/**
	 * 添加保存供应商
	 * 
	 * @param bean
	 * @param request
	 * @param model
	 * @param merchantid
	 * @return
	 */
	@RequestMapping(value = "/suppliersave.jspx")
	public String save(ShopSupplier bean, HttpServletRequest request,
			ModelMap model, int merchantid) {

		bean.setMerchantid(merchantid + "");
		bean = manager.save(bean);
		Website web = SiteUtils.getWeb(request);
		return FrontHelper.showSuccess("操作成功", "supplieradd.jspx?merchantid="
				+ merchantid, web, model, request);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param pageNo
	 * @param request
	 * @param model
	 * @param merchantid
	 * @return
	 */
	@RequestMapping(value = "/supplierdelete.jspx")
	public String delete(Long id, Integer pageNo, HttpServletRequest request,
			ModelMap model, int merchantid) {
		manager.deleteById(id);
		return list(pageNo, request, model, merchantid);
	}

	/**
	 * 修改
	 * 
	 * @param id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/supplieredit.jspx")
	public String edit(Long id, HttpServletRequest request, ModelMap model,
			int merchantid) {
		model.addAttribute("merchantid", merchantid);
		model.addAttribute("shopSupplier", manager.findById(id));
		Website web = SiteUtils.getWeb(request);
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(SHOP_SYS, MessageResolver.getMessage(request,
				SUPPLIEREDIT), request);
	}

	/**
	 * 修改保存
	 * 
	 * @param bean
	 * @param pageNo
	 * @param request
	 * @param model
	 * @param merchantid
	 * @return
	 */
	@RequestMapping("/supplierupdatesave.jspx")
	public String updatesave(ShopSupplier bean, Integer pageNo,
			HttpServletRequest request, ModelMap model, int merchantid) {
		bean = manager.update(bean);
		return list(pageNo, request, model, merchantid);
	}

	@Autowired
	private ShopSupplierMng manager;

}
